#include "ChatInterface.h"
#include "ui_ChatInterface.h"
#include "ui_friendsitem.h"
#include <QListWidgetItem>
#include <QJsonDocument>
#include <QJsonObject>
#include <QMouseEvent>
#include <QJsonArray>
#include <QBuffer>
#include <component/friendsitem.h>
#include<vector>

ChatInterface::ChatInterface(int current_id,QWidget *parent)
    : QWidget(parent), current_id(current_id), ui(new Ui::ChatInterface)
{
    friend_request = new Friendrequest(this);
    setWindowTitle("微讯");
    setWindowFlags(Qt::FramelessWindowHint | Qt::WindowMinimizeButtonHint);
    ui->setupUi(this);
    friend_request->friendInformation(current_id);
    connect(friend_request,&Friendrequest::friendFinished,this,&ChatInterface::onFriendFinished);
}

ChatInterface::~ChatInterface()
{
    delete ui;
}

void ChatInterface::mouseMoveEvent(QMouseEvent *e)
{
    if(is_drag){
        move(e->globalPosition().toPoint() - start_pos);
    }
}

void ChatInterface::mousePressEvent(QMouseEvent *e)
{
    start_pos = e->pos();
    if(start_pos.y() < 30){
        is_drag = true;
    }else{
        is_drag = false;
    }
}

void ChatInterface::onFriendFinished(MyNamespace::Result<std::vector<FriendInfo>> friend_list)
{
    std::vector<FriendInfo> friendList = friend_list.getData();
    for(FriendInfo itemValue : friendList)
    {
        int id = itemValue.getId();
        QString name = itemValue.getName();
        qDebug()<<name;
        QString avatarUrl = itemValue.getUrl();
        qDebug()<<avatarUrl;
        //创建好友列表项
        friendsitem *friItem= new friendsitem(id,name,avatarUrl);
        //QListWidgetItem
        QListWidgetItem *m_Item = new QListWidgetItem(ui->listWidget);
        //设置管理项，m_Item的宽高
        m_Item->setSizeHint(QSize(281,50));
        //将自定义的QWidge--friItem，设置为m-Item的小组件。
        ui->listWidget->setItemWidget(m_Item,friItem);
    }
}

void ChatInterface::on_myInformation_clicked()
{
    ui->stackedWidget->setCurrentIndex(1);
}

void ChatInterface::on_pushButton_clicked()
{
    ui->stackedWidget->setCurrentIndex(0);
}

void ChatInterface::on_toolButton_4_clicked()
{
     this->showMinimized();
}

void ChatInterface::on_toolButton_3_clicked()
{
    this->close();
}

void ChatInterface::on_toolButton_2_clicked()
{
    this->showMinimized();
}

void ChatInterface::on_toolButton_clicked()
{
     this->close();
}

void ChatInterface::on_pushButton_7_clicked()
{
    set = new Settings;
    set->show();
}

